<template>
  <div class="flex h-full flex-col overflow-hidden">
    <!-- 顶部退出按钮 -->
    <div class="header-bar">
      <div class="header-title">绕线机控制系统</div>
      <button @click="logout" class="logout-button">
        <i class="fas fa-sign-out-alt"></i>
        退出
      </button>
    </div>
    <main class="flex-grow p-6 grid grid-cols-3 gap-6 overflow-y-auto">
      <div id="nav-status" class="nav-card" @click="navigateTo('run-status', $event)">
        <i class="fas fa-tachometer-alt text-blue-500"></i>
        <span class="nav-card-title">运行状态</span>
        <p class="nav-card-subtitle">查看实时状态和操作设备</p>
      </div>
      <div id="nav-mes" class="nav-card" @click="navigateTo('mes-log', $event)">
        <i class="fas fa-network-wired text-teal-500"></i>
        <span class="nav-card-title">MES 通讯</span>
        <p class="nav-card-subtitle">监控与MES的交互日志</p>
      </div>
<!--      <div id="nav-logs" class="nav-card" @click="navigateTo('alarm-log', $event)">-->
<!--        <i class="fas fa-file-alt text-orange-500"></i>-->
<!--        <span class="nav-card-title">报警日志</span>-->
<!--        <p class="nav-card-subtitle">查询设备报警和事件记录</p>-->
<!--      </div>-->
      <div id="nav-chart" class="nav-card" @click="navigateTo('chart', $event)">
        <i class="fas fa-chart-line text-indigo-500"></i>
        <span class="nav-card-title">地址监控</span>
        <p class="nav-card-subtitle">实时观察PLC地址数值变化</p>
      </div>
      <div id="nav-settings" class="nav-card" @click="navigateTo('settings-nav', $event)">
        <i class="fas fa-cogs text-gray-500"></i>
        <span class="nav-card-title">系统设置</span>
        <p class="nav-card-subtitle">配置通讯、事件、报警等参数</p>
      </div>
      <div id="nav-mes-mock" class="nav-card" @click="navigateTo('mes-mock', $event)">
        <i class="fas fa-server text-gray-500"></i>
        <span class="nav-card-title">MES 模拟服务器</span>
        <p class="nav-card-subtitle">模拟 MES 服务器</p>
      </div>
    </main>
  </div>
</template>

<script setup>
import {useRouter} from 'vue-router';

const router = useRouter(); // 获取路由实例

const navigateTo = (targetRoute, event) => {
  if (targetRoute) {
    const cardElement = event.currentTarget; // 获取点击的卡片元素
    if (cardElement) {
      cardElement.classList.add('ring-2', 'ring-blue-400', 'ring-offset-2', 'ring-offset-gray-100'); // 使用 gray-100 代替 f0f4f8
      setTimeout(() => {
        cardElement.classList.remove('ring-2', 'ring-blue-400', 'ring-offset-2', 'ring-offset-gray-100');
        router.push('/' + targetRoute);
      }, 200); // 动画持续时间
    } else {
      router.push('/' + targetRoute);
    }
  } else {
    console.error(`未知页面key: ${pageKey}`);
  }
};

// 退出登录
const logout = () => {
  // 清除本地存储的配置信息（可选）
  // localStorage.removeItem('interfaceConfig');
  
  // 跳转到登录页（根路径）
  router.push('/');
};
</script>

<style scoped>
/* 顶部栏样式 */
.header-bar {
  background-color: var(--bg-secondary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.06);
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
}

.logout-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background-color: #dc2626;
  color: white;
  border: none;
  border-radius: 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.logout-button:hover {
  background-color: #b91c1c;
  transform: translateY(-1px);
}

.logout-button:active {
  transform: translateY(0);
}

header {
  background-color: var(--bg-secondary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.06);
  /* padding: 0.75rem 1.25rem; */
  border-bottom: 1px solid var(--border-color);
  /* display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.5rem; */
}

.header-left, .header-right {
  /* display: flex; align-items: center; gap: 0.75rem; */
}

.header-info-group {
  /* display: flex; align-items: baseline; gap: 0.75rem; border-right: 1px solid var(--border-color); padding-right: 0.75rem; flex-wrap: nowrap; white-space: nowrap; */
}

.hmi-label {
  font-size: 0.7rem; /* Slightly smaller label */
  font-weight: 500;
  color: var(--text-secondary);
  margin-right: 0.15rem; /* Space between label and value */
}

.hmi-value {
  font-size: 0.8rem; /* Slightly smaller value */
  font-weight: 600;
  color: var(--text-primary);
}

.status-indicator {
  /* display: flex; align-items: center; gap: 0.25rem; */
}

.status-light {
  width: 12px; /* Smaller lights */
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
  flex-shrink: 0;
}

.light-red {
  background-color: var(--status-red);
  box-shadow: 0 0 5px 1px var(--status-red);
}

.light-yellow {
  background-color: var(--status-yellow);
  box-shadow: 0 0 5px 1px var(--status-yellow);
}

.light-green {
  background-color: var(--status-green);
  box-shadow: 0 0 5px 1px var(--status-green);
}

.light-off {
  background-color: var(--status-off);
  opacity: 0.5;
}

#machine-status-text {
  /* font-size: 0.75rem; font-weight: 700; padding: 0.1rem 0.4rem; border-radius: 9999px; background-color: rgba(0,0,0,0.05); white-space: nowrap; flex-shrink: 0; */
}

.status-run {
  color: var(--status-green);
  background-color: rgba(34, 197, 94, 0.1);
}

.status-stop {
  color: var(--status-red);
  background-color: rgba(239, 68, 68, 0.1);
}

.status-idle {
  color: var(--status-yellow);
  background-color: rgba(234, 179, 8, 0.1);
}


#current-time {
  /* font-size: 0.75rem; color: var(--text-secondary); font-weight: 500; white-space: nowrap; flex-shrink: 0; */
}

/* MES connection status dot - add this style */
.connection-status {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-left: 0.25rem; /* Space after icon */
  transition: background-color 0.3s ease;
}

.connection-online {
  background-color: var(--online-green); /* Assuming var --online-green is defined */
}

.connection-offline {
  background-color: var(--offline-red); /* Assuming var --offline-red is defined */
}


/* --- Main Content Area --- */
main {
  flex-grow: 1;
  /* flex-grow: 1; padding: 1.5rem; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); gap: 1.5rem; overflow-y: auto; */
}

/* --- Navigation Card Styling --- */
.nav-card {
  background-color: var(--bg-secondary);
  border-radius: 0.75rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  border: 1px solid var(--border-color);
  cursor: pointer;
  transition: transform 0.2s ease-out, box-shadow 0.2s ease-out, border-color 0.2s ease-out;
  min-height: 110px;
}

.nav-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.07), 0 4px 6px -4px rgba(0, 0, 0, 0.07);
  border-color: var(--accent-blue);
}

.nav-card i {
  font-size: 2.25rem;
  margin-bottom: 0.75rem;
  transition: color 0.2s ease;
}

/* Specific Icon Colors - Using CSS variables */
#nav-status i {
  color: var(--accent-blue);
}

#nav-mes i {
  color: var(--accent-teal);
}

#nav-logs i {
  color: var(--accent-orange);
}

#nav-settings i {
  color: var(--accent-gray);
}

#nav-chart i {
  color: var(--accent-indigo);
}


.nav-card-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-top: 0.25rem;
}

.nav-card-subtitle {
  font-size: 0.65rem;
  color: var(--text-tertiary);
  margin-top: 0.15rem;
  line-height: 1.2;
  max-width: 90%;
}

.nav-card:hover .nav-card-title {
  color: var(--accent-blue);
}

</style>